package org.thirdHero.wx.request;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.binarywang.wxpay.bean.request.WxPayBaseRequest;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.google.common.base.Strings;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
import me.chanjar.weixin.common.annotation.Required;
import org.apache.commons.lang3.StringUtils;
import org.thirdHero.wx.constant.WxProFitSharingConstant;
import org.thirdHero.wx.utils.WxSignUtils;

import java.util.List;

/**
 * 单次分账请求按照传入的分账接收方账号和资金进行分账，
 * 同时会将订单剩余的待分账金额解冻给本商户。故操作成功后，
 * 订单不能再进行分账，也不能进行分账完结。
 */
@XStreamAlias("xml")
public class WxProfitSharingRequest extends WxProfitSharingBaseRequest {
    /**
     * <pre>
     * 字段名：微信订单号
     * 变量名：transaction_id
     * 是否必填：是
     * 类型：string(32)
     * 示例值：4208450740201411110007820472
     * 描述：MERCHANT_ID：微信支付订单号
     * </pre>
     */
    @XStreamAlias("transaction_id")
    private String transactionId;
    /**
     * <pre>
     * 字段名：商户分账单号
     * 变量名：out_order_no
     * 是否必填：是
     * 类型：string(64)
     * 示例值：P20150806125346
     * 描述：商户系统内部的分账单号，在商户系统内部唯一
     * （单次分账、多次分账、完结分账应使用不同的商户分账单号），
     * 同一分账单号多次请求等同一次。只能是数字、大小写字母_-|*@
     * </pre>
     */
    @XStreamAlias("out_order_no")
    private String outOrderNo;
    /**
     * <pre>
     * 字段名：分账接收方列表
     * 变量名：receivers
     * 是否必填：是
     * 类型：String(10240)
     * 示例值：
     * [
     *     {
     *          "type": "MERCHANT_ID",
     *          "account":"190001001",
     *          "amount":100,
     *          "description": "分到商户"
     * },
     *     {
     *          "type": "PERSONAL_WECHATID",
     *          "account":"86693952",
     *          "amount":888,
     *          "description": "分到个人"
     * }
     * ]
     * 描述：
     * 分账接收方列表，不超过50个json对象，不能设置分账方作为分账接受方
     * 点击行前的+展开字段详情
     * </pre>
     */
    @Required
    @XStreamAlias("receivers")
    private String receivers;



    @XStreamOmitField
    private List<WxProfitSharingReceivers> wxProfitSharingReceivers;
    @Override
    protected void checkConstraints() throws WxPayException {
        if(Strings.isNullOrEmpty(receivers)){
           throw new WxPayException("分账接收方列表必须输入");
        }
    }


    public String getTransactionId() {
        return transactionId;
    }

    public void setTransactionId(String transactionId) {
        this.transactionId = transactionId;
    }

    public String getOutOrderNo() {
        return outOrderNo;
    }

    public void setOutOrderNo(String outOrderNo) {
        this.outOrderNo = outOrderNo;
    }

    public String getReceivers() {
        return receivers;
    }




    public void setReceivers(String receivers) {
        this.receivers = receivers;
    }

    public void setWxProfitSharingReceivers(List<WxProfitSharingReceivers> wxProfitSharingReceivers) {
        this.wxProfitSharingReceivers = wxProfitSharingReceivers;
        this.receivers = WxProFitSharingConstant.CDATA_PREFIX+ JSONArray.toJSONString(wxProfitSharingReceivers)+WxProFitSharingConstant.CDATA_SUFFIX;

    }






}
